package com.alan.service.impl;

import com.alan.constants.SystemConstants;
import com.alan.domain.ResponseResult;
import com.alan.domain.entity.Comment;
import com.alan.domain.entity.LoginUser;
import com.alan.domain.vo.CommentVo;
import com.alan.domain.vo.PageVo;
import com.alan.mapper.CommentMapper;
import com.alan.mapper.UserMapper;
import com.alan.service.CommentService;
import com.alan.service.UserService;
import com.alan.utils.BeanCopyUtils;
import com.alan.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-09-18 10:45:43
 */
@Service("commentService")
public class CommentServiceImpl
        extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Resource
    private UserService userService;

    @Override
    public ResponseResult commentList(String type,Long articleId, Integer pageNum, Integer pageSize) {
        //判断是文章评论还是友链评论

        //查询对应文章根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(type),Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getType,type);
        queryWrapper.eq(Comment::getRootId,-1);

        //分页查询
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        //封装Vo
        List<Comment> commentList = page.getRecords();
        List<CommentVo> commentVos = toCommentVos(commentList);

        /**
         *  查询子评论
         */
        for (CommentVo commentVo : commentVos) {
            Long id = commentVo.getId();//根评论id
            List<CommentVo> childCommentVos = getChild(id);//得到子评论集合
            commentVo.setChildren(childCommentVos);
        }




        PageVo pageVo = new PageVo(commentVos, page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        //补充comment中的属性
        //前端只传来了部分的属性
        //LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //太长，封装一下
        //Long userId = SecurityUtils.getUserId();
        //comment.setCreateBy(userId);
        /**
         * 利用MP的自动填充
         */
        //TODO： 对评论进行筛选，敏感词处理
        save(comment);
        return ResponseResult.okResult();
    }

    private List<CommentVo> getChild(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getToCommentId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);//按时间排序
        List<Comment> commentList = list(queryWrapper);
        return toCommentVos(commentList);

    }

    private List<CommentVo> toCommentVos(List<Comment> commentList){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(commentList, CommentVo.class);
        //添加缺少的属性
        //通过createBy（id）用户的昵称
        //通过toCommentUserId回复人的昵称
        for (CommentVo commentVo : commentVos) {
            Long createBy = commentVo.getCreateBy();
            Long toCommentUserId = commentVo.getToCommentUserId();

            //评论人
            String createGuy = userService.getById(createBy).getNickName();
            commentVo.setUsername(createGuy);
            //回复人
            if (toCommentUserId!=-1){
                String commentedGuy = userService.getById(toCommentUserId).getNickName();
                commentVo.setToCommentUserName(commentedGuy);
            }

        }
        return commentVos;
    }


}

